import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let lifeData = {};

try{
	// 尝试获取本地是否存在lifeData变量，第一次启动APP时是不存在的
	lifeData = uni.getStorageSync('lifeData');
}catch(e){
	
}

// 需要永久存储，且下次APP启动需要取出的，在state中的变量名
let saveStateKeys = ['vuex_user', 'vuex_token', 'typeList', 'recordList', 'connList', 'myConfig', 'headImgList'];

// 保存变量到本地存储中
const saveLifeData = function(key, value){
	// 判断变量名是否在需要存储的数组中
	if(saveStateKeys.indexOf(key) != -1) {
		// 获取本地存储的lifeData对象，将变量添加到对象中
		let tmp = uni.getStorageSync('lifeData');
		// 第一次打开APP，不存在lifeData变量，故放一个{}空对象
		tmp = tmp ? tmp : {};
		tmp[key] = value;
		// 执行这一步后，所有需要存储的变量，都挂载在本地的lifeData对象中
		uni.setStorageSync('lifeData', tmp);
	}
}
const store = new Vuex.Store({
	state: {
		// 如果上面从本地获取的lifeData对象下有对应的属性，就赋值给state中对应的变量
		// 加上vuex_前缀，是防止变量名冲突，也让人一目了然
		vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {name: '明月'},
		vuex_token: lifeData.vuex_token ? lifeData.vuex_token : '',
		myConfig: lifeData.myConfig ? lifeData.myConfig : {
															"baseUrl": "http://124.71.235.241:30098/mini-ledger",
															// "baseUrl": "http://localhost:9007/mini-ledger",
															// "baseUrl": "http://192.168.0.103:9007/mini-ledger/",
															"showTotal": true,
															"autoUpdate": true,
															"version": "V1.0.4",
															"userId": "",
															"userName": "点击登录",
															"img": "",
															"phone": "",
															"passWord": "",},
		connList: lifeData.connList ? lifeData.connList : [],
		recordList: lifeData.recordList ? lifeData.recordList : [],
		headImgList: lifeData.headImgList ? lifeData.headImgList : [ "/static/img/1.jpg",
																	"/static/img/2.jpg",
																	"/static/img/3.jpg",
																	"/static/img/4.jpg",
																	"/static/img/5.png",
																	"/static/img/6.png",
																	"/static/img/7.png",
																	"/static/img/8.jpg",
																	"/static/img/9.jpg",
																	"/static/img/10.jpg",
																	"/static/img/11.jpg",
																	"/static/img/12.jpg",
																	"/static/img/13.jpg",
																	"/static/img/14.jpg",
																	"/static/img/15.jpg",
																	"/static/img/16.jpg"],
		typeList: lifeData.typeList ? lifeData.typeList : [ {code: "canyin", name: "餐饮", type:0, icon:"canyin"},
															{code: "shuiguo", name: "水果", type:0, icon:"shuiguo"},
															{code: "shucai", name: "蔬菜", type:0, icon:"shucai"},
															{code: "fushi", name: "服饰", type:0, icon:"fushi"},
															{code: "riyongpin", name: "日用", type:0, icon:"riyongpin"},
															{code: "jiaotong", name: "交通", type:0, icon:"jiaotong"},
															{code: "yiliao", name: "医疗", type:0, icon:"yiliao"},
															{code: "fumu", name: "父母", type:0, icon:"fumu"},
															{code: "zinvhuafei", name: "育儿", type:0, icon:"zinvhuafei"},
															{code: "jiaoyu", name: "教育", type:0, icon:"jiaoyu"},
															{code: "yule", name: "娱乐", type:0, icon:"yule"},
															{code: "baoxian", name: "保险", type:0, icon:"baoxian"},
															{code: "fangdai", name: "房贷", type:0, icon:"fangdai"},
															{code: "qitazhichu", name: "其他", type:0, icon:"qitazhichu"},
															{code: "gongzi", name: "工资", type:1, icon:"gongzi"},
															{code: "jiangjin", name: "奖金", type:1, icon:"jiangjin"},
															{code: "hongbao", name: "红包", type:1, icon:"hongbao"},
															{code: "baoxiao", name: "报销", type:1, icon:"baoxiao"},
															{code: "jianzhi", name: "兼职", type:1, icon:"jianzhi"},
															{code: "licai", name: "理财", type:1, icon:"licai"},
															{code: "qitashouru", name: "其他", type:1, icon:"qitashouru"}],
		// 如果vuex_version无需保存到本地永久存储，无需lifeData.vuex_version方式
		vuex_version: '1.0.1',
		vuex_demo: '绛紫',
		// 自定义tabbar数据
		vuex_tabbar: [{
				iconPath: "/static/uview/example/component.png",
				selectedIconPath: "/static/uview/example/component_select.png",
				text: '组件',
				pagePath: '/pages/example/components'
			},
			{
				iconPath: "/static/uview/example/js.png",
				selectedIconPath: "/static/uview/example/js_select.png",
				text: '工具',
				midButton: true,
				pagePath: '/pages/example/js'
			},
			{
				iconPath: "/static/uview/example/template.png",
				selectedIconPath: "/static/uview/example/template_select.png",
				text: '模板',
				pagePath: '/pages/example/template'
			}
		]
	},
	mutations: {
		$uStore(state, payload) {
			// 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
			let nameArr = payload.name.split('.');
			let saveKey = '';
			let len = nameArr.length;
			if(len >= 2) {
				let obj = state[nameArr[0]];
				for(let i = 1; i < len - 1; i ++) {
					obj = obj[nameArr[i]];
				}
				obj[nameArr[len - 1]] = payload.value;
				saveKey = nameArr[0];
			} else {
				// 单层级变量，在state就是一个普通变量的情况
				state[payload.name] = payload.value;
				saveKey = payload.name;
			}
			// 保存变量到本地，见顶部函数定义
			saveLifeData(saveKey, state[saveKey])
		}
	}
})

export default store
